package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.ErpContractCustomer;
import com.monitor.service.ErpContractCustomerService;
import com.monitor.service.ErpCustomerService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 合同协议缔约客户表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-17
 */
@RestController
@RequestMapping("/ErpContractCustomerCustomer")
@Api(description="合同协议-缔约客户")
public class ErpContractCustomerController {

    @Resource
    private ErpContractCustomerService contractCustomerService;
    @Resource
    private ErpCustomerService customerService;

    @RequestMapping("/inertOrUpdateContractCustomer")
    @ApiOperation("添加Or修改缔约客户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateContractCustomer(ErpContractCustomer contract) {
        Result<Integer> result=new Result<Integer>();
        try{
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = formatter.format(currentTime);
            contract.setContractingDate(dateString);
            contract.setInputPerson(customerService.selectById(contract.getInputId()).getCusName());
            result.setCount(1);
            result.setData(contractCustomerService.insertOrUpdate(contract)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getContractCustomerById")
    @ApiOperation("根据缔约客户id缔约客户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "协议ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpContractCustomer> getContractCustomerById(Integer contractId) {
        Result<ErpContractCustomer> result=new Result<ErpContractCustomer>();
        try{
            ErpContractCustomer erpContractCustomer = contractCustomerService.selectById(contractId);

            String contractingDate = erpContractCustomer.getContractingDate();
            SimpleDateFormat    sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    erpContractCustomer.setDate(contractingDate);
            result.setCount(1);
            result.setData(erpContractCustomer);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removeContractCustomerById")
    @ApiOperation("根据协议id删除缔约客户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "协议ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeContractCustomerById(Integer contractId) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(contractCustomerService.deleteById(contractId)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getContractCustomerBycontractIdPage")
    @ApiOperation("根据协议ID查询缔约客户分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "合同ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Page<ErpContractCustomer>> getContractCustomerBycustIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                      @RequestParam(value="limit",defaultValue="10") int limit, Integer contractId) {
        Result<Page<ErpContractCustomer>> result=new Result<Page<ErpContractCustomer>>();
        try{
            Page<ErpContractCustomer> page1=new Page<ErpContractCustomer>(page,limit);
            Page<ErpContractCustomer> carPage=contractCustomerService.selectPage(page1,new EntityWrapper<ErpContractCustomer>().eq("contract_id",contractId));
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getContractCustomerBycontractIdList")
    @ApiOperation("根据协议ID查询所有缔约客户")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "合同ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpContractCustomer>> getContractCustomerBycustIdList(Integer contractId) {
        Result<List<ErpContractCustomer>> result=new Result<List<ErpContractCustomer>>();
        try{

            List<ErpContractCustomer> carPage=contractCustomerService.selectList(new EntityWrapper<ErpContractCustomer>().eq("contract_id",contractId));
            for (int i = 0; i < carPage.size(); i++) {
                ErpContractCustomer erpContractCustomer =  carPage.get(i);
                String contractingDate = erpContractCustomer.getContractingDate();
                SimpleDateFormat    sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                erpContractCustomer.setDate(contractingDate);
            }
            result.setCount(carPage.size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

}

